package com.ruike.alisurface.utils;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
import com.voodoo.lib_utils.L;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class AdvDialogUtils {

    static Dialog dialogs;//显示弹窗
    static Timer timer;//计时
    static TimerTask timerTask;

    public static void AliHuoDongDialog(Context context, List<String> aldatas) {
        try {
            if (dialogs != null && dialogs.isShowing()) {

                return;
            }
            cancelTimer();
            //1、使用Dialog、设置style
            dialogs = new Dialog(context, R.style.Dialog);
            List<String> advList = new ArrayList<>();
            for (int i = 0; i < aldatas.size(); i++) {
                String Name = aldatas.get(i).substring(aldatas.get(i).lastIndexOf("/")).substring(1);
                File file = new File(Constant.topAdvpath, Name);
                if (file.exists()) {
                    advList.add(file.getAbsolutePath());
                } else {
                    advList.add(aldatas.get(i));
                }
            }
            L.i("top广告图片地址==", advList.toString());
//2、设置布局
            View view = View.inflate(context, R.layout.dialog_custom_layout, null);
            Banner activityBannerBanner = view.findViewById(R.id.dl_Banner_banner);
            ImageView dl_delect = view.findViewById(R.id.dl_delect);
            activityBannerBanner.setIndicatorGravity(BannerConfig.CIRCLE_INDICATOR);

            // 设置图片加载器
            activityBannerBanner.setImageLoader(new BannerImageLoader());
            // 设置banner样式
            activityBannerBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            // 设置图片集合
            activityBannerBanner.setImages(advList);
            // 设置自动轮播，默认为true
            activityBannerBanner.isAutoPlay(advList.size() > 1);
            // 设置轮播时间单位毫秒
            activityBannerBanner.setDelayTime(3000);
            activityBannerBanner.start();
            dialogs.setContentView(view);
            dialogs.setCancelable(false);
            Window window = dialogs.getWindow();
            //设置弹出位置gh
            window.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams lp = window.getAttributes();
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            if (windowManager != null) {
                DisplayMetrics outMetrics = new DisplayMetrics();
                windowManager.getDefaultDisplay().getMetrics(outMetrics);
                int widthPixels = outMetrics.widthPixels;
                int heightPixels = outMetrics.heightPixels;
                L.i("widthPixels==heightPixels", widthPixels, heightPixels);
                lp.width = (int) (widthPixels * 0.7); // 宽度设置为屏幕的0.6
                lp.height = (int) (heightPixels * 0.7); // 高度设置为屏幕的0.6

//                lp.width=DensityUtil.dp2px(context,600);
//                lp.height=DensityUtil.dp2px(context,1166);
            }
            window.setAttributes(lp);
            //设置对话框大小
//        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            dialogs.show();

            dl_delect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    dlCancal();
                }
            });
            timer = new Timer();//计时
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    L.i("倒计时结束，开始取消弹框");
                    dlCancal();
                }
            };
            timer.schedule(timerTask, 60_000);
           /* dialogs.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (activityBannerBanner != null) {
                        activityBannerBanner.stopAutoPlay();
                        activityBannerBanner.releaseBanner();
                    }
                }
            });*/

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void dlCancal() {
        if (dialogs != null && dialogs.isShowing()) {
            L.i("倒计时结束，取消弹框");
            dialogs.dismiss();
        }
        cancelTimer();
    }

    public static void cancelTimer() {
        L.i("倒计时结束，取消计时");
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
        if (timer != null) {
            timer.purge();
            timer.cancel();
            timer = null;
        }
    }
}
